import numpy as np
import matplotlib.pyplot as plt

def verify_equivalence():
    """验证可微与可导的等价性"""
    
    # 测试多个函数
    test_functions = [
        ("x²", lambda x: x**2, lambda x: 2*x),
        ("sin(x)", np.sin, np.cos),
        ("e^x", np.exp, np.exp),
        ("ln(x)", lambda x: np.log(x) if x > 0 else np.nan, lambda x: 1/x if x > 0 else np.nan)
    ]
    
    x0 = 1
    dx = 0.001
    
    print("可微与可导等价性验证")
    print("=" * 50)
    
    for name, func, deriv in test_functions:
        # 通过导数计算微分
        dy_through_derivative = deriv(x0) * dx
        
        # 通过定义计算微分
        delta_y = func(x0 + dx) - func(x0)
        # 对于可微函数，A = f'(x0)
        dy_through_definition = deriv(x0) * dx
        
        error = abs(dy_through_derivative - dy_through_definition)
        
        print(f"{name}:")
        print(f"  通过导数: dy = f'({x0})·dx = {deriv(x0):.4f} × {dx} = {dy_through_derivative:.6f}")
        print(f"  通过定义: dy = A·dx = {dy_through_definition:.6f}")
        print(f"  误差: {error:.10f}")
        print()

verify_equivalence()